package com.eastfair.certificate.util;

import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.Security;


@Component
public class PBE_Cipher {
    //
    public static final String CIPHER_NAME = "PBEwithSHA1and128bitAES-CBC-BC";
    public static final String PASS_WORD = "12312JKL12J12LK3J2UISOFSN214KL23YDFDBCVUCNERWEREIVCBDFKWKERNSJFSLFDSJIU7892H2K33YJGDSKFSD1";

    public static String macAddress;

    @Value("${com.eastfair.mac.address}")
    public void setMacAddress(String macAddress) {
        PBE_Cipher.macAddress = macAddress;
    }

    //加密
    public static byte[] encrypt(byte[] input) throws Exception {
        byte[] salt = getMacAddrTest().getBytes("utf-8");
        PBEKeySpec keySpec = new PBEKeySpec(PASS_WORD.toCharArray());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(CIPHER_NAME);
        SecretKey secretKey = keyFactory.generateSecret(keySpec);
        PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 5000);
        Cipher cipher = Cipher.getInstance(CIPHER_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
        byte[] bytes = cipher.doFinal(input);
        return bytes;
    }

    //解密
    public static byte[] decrypt(byte[] input) throws Exception {
        byte[] salt = getMacAddrTest().getBytes("utf-8");
        PBEKeySpec keySpec = new PBEKeySpec(PASS_WORD.toCharArray());
        SecretKey secretKey = SecretKeyFactory.getInstance(CIPHER_NAME).generateSecret(keySpec);
        PBEParameterSpec spec = new PBEParameterSpec(salt, 5000);
        Cipher cipher = Cipher.getInstance(CIPHER_NAME);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, spec);
        byte[] bytes = cipher.doFinal(input);
        return bytes;
    }


    private static String getMacAddrTest() throws UnknownHostException, SocketException {
        if (StrUtil.isNotBlank(macAddress)) {
            return macAddress;
        }
        NetworkInterface netInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
        byte[] macAddr = netInterface.getHardwareAddress();

        StringBuilder sb = new StringBuilder();
        for (byte b : macAddr) {
            sb.append(toHexString(b));
        }
        return sb.toString();
    }

    private static String toHexString(int integer) {

// 将得来的int类型数字转化为十六进制数

        String str = Integer.toHexString((int) (integer & 0xff));

// 如果遇到单字符，前置0占位补满两格

        if (str.length() == 1) {

            str = "0" + str;

        }

        return str;

    }


    public static void main(String[] args) {
        try {
            String key = "1";
            String cipher = "123123";
            Security.addProvider(new BouncyCastleProvider());
            byte[] decrypt = PBE_Cipher.decrypt(HexUtil.decodeHex(cipher));
            cipher = new String(decrypt);
            String s= SecureUtil.aes(cipher.getBytes()).encryptHex(key.toString());
            System.out.println(s);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
